﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:Enter
	/// </summary>
	public partial class Enter
	{
		public Enter()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("ID", "AD_Enter"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from AD_Enter");
			strSql.Append(" where ID=@ID");
			OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
			parameters[0].Value = ID;

			return DbHelperOleDb.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Maticsoft.Model.Enter model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into AD_Enter(");
			strSql.Append("ImgName,ImgURL,StartTime,EndTime,Link,clCount,RouterID)");
			strSql.Append(" values (");
			strSql.Append("@ImgName,@ImgURL,@StartTime,@EndTime,@Link,@clCount,@RouterID)");
			OleDbParameter[] parameters = {
					new OleDbParameter("@ImgName", OleDbType.VarChar,0),
					new OleDbParameter("@ImgURL", OleDbType.VarChar,0),
					new OleDbParameter("@StartTime", OleDbType.Date),
					new OleDbParameter("@EndTime", OleDbType.Date),
					new OleDbParameter("@Link", OleDbType.VarChar,0),
					new OleDbParameter("@clCount", OleDbType.Integer,4),
					new OleDbParameter("@RouterID", OleDbType.VarChar,0)};
			parameters[0].Value = model.ImgName;
			parameters[1].Value = model.ImgURL;
			parameters[2].Value = model.StartTime;
			parameters[3].Value = model.EndTime;
			parameters[4].Value = model.Link;
			parameters[5].Value = model.clCount;
			parameters[6].Value = model.RouterID;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.Enter model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update AD_Enter set ");
			strSql.Append("ImgName=@ImgName,");
			strSql.Append("ImgURL=@ImgURL,");
			strSql.Append("StartTime=@StartTime,");
			strSql.Append("EndTime=@EndTime,");
			strSql.Append("Link=@Link,");
			strSql.Append("clCount=@clCount,");
			strSql.Append("RouterID=@RouterID");
			strSql.Append(" where ID=@ID");
			OleDbParameter[] parameters = {
					new OleDbParameter("@ImgName", OleDbType.VarChar,0),
					new OleDbParameter("@ImgURL", OleDbType.VarChar,0),
					new OleDbParameter("@StartTime", OleDbType.Date),
					new OleDbParameter("@EndTime", OleDbType.Date),
					new OleDbParameter("@Link", OleDbType.VarChar,0),
					new OleDbParameter("@clCount", OleDbType.Integer,4),
					new OleDbParameter("@RouterID", OleDbType.VarChar,0),
					new OleDbParameter("@ID", OleDbType.Integer,4)};
			parameters[0].Value = model.ImgName;
			parameters[1].Value = model.ImgURL;
			parameters[2].Value = model.StartTime;
			parameters[3].Value = model.EndTime;
			parameters[4].Value = model.Link;
			parameters[5].Value = model.clCount;
			parameters[6].Value = model.RouterID;
			parameters[7].Value = model.ID;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from AD_Enter ");
			strSql.Append(" where ID=@ID");
			OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
			parameters[0].Value = ID;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from AD_Enter ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Enter GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,ImgName,ImgURL,StartTime,EndTime,Link,clCount,RouterID from AD_Enter ");
			strSql.Append(" where ID=@ID");
			OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
			parameters[0].Value = ID;

			Maticsoft.Model.Enter model=new Maticsoft.Model.Enter();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ImgName"]!=null && ds.Tables[0].Rows[0]["ImgName"].ToString()!="")
				{
					model.ImgName=ds.Tables[0].Rows[0]["ImgName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ImgURL"]!=null && ds.Tables[0].Rows[0]["ImgURL"].ToString()!="")
				{
					model.ImgURL=ds.Tables[0].Rows[0]["ImgURL"].ToString();
				}
				if(ds.Tables[0].Rows[0]["StartTime"]!=null && ds.Tables[0].Rows[0]["StartTime"].ToString()!="")
				{
					model.StartTime=DateTime.Parse(ds.Tables[0].Rows[0]["StartTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["EndTime"]!=null && ds.Tables[0].Rows[0]["EndTime"].ToString()!="")
				{
					model.EndTime=DateTime.Parse(ds.Tables[0].Rows[0]["EndTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Link"]!=null && ds.Tables[0].Rows[0]["Link"].ToString()!="")
				{
					model.Link=ds.Tables[0].Rows[0]["Link"].ToString();
				}
				if(ds.Tables[0].Rows[0]["clCount"]!=null && ds.Tables[0].Rows[0]["clCount"].ToString()!="")
				{
					model.clCount=int.Parse(ds.Tables[0].Rows[0]["clCount"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RouterID"]!=null && ds.Tables[0].Rows[0]["RouterID"].ToString()!="")
				{
					model.RouterID=ds.Tables[0].Rows[0]["RouterID"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,ImgName,ImgURL,StartTime,EndTime,Link,clCount,RouterID ");
			strSql.Append(" FROM AD_Enter ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList1(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.ID,a.ImgName,a.ImgURL,a.StartTime,a.EndTime,a.Link,a.clCount,a.RouterID,b.address ");
            strSql.Append(" FROM AD_Enter a ,AD_RouterInfo b where a.RouterID=b.code ");
            return DbHelperOleDb.Query(strSql.ToString());
        }

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM AD_Enter ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from AD_Enter T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "AD_Enter";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

